package com.vhuomai.order.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.vhuomai.order.entity.BaseCart;
import com.vhuomai.order.service.CartService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "购物车")
@RestController
@Scope(role = UserRole.USER)
@RequestMapping("/api")
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation(value = "添加购物车")
    @PostMapping(value = "/cart")
    public ServiceResult <Boolean> addCart(@RequestBody BaseCart cart, Principal user) {
        cartService.addCart(cart, user.getId());
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除购物车")
    @DeleteMapping(value = "/cart")
    public ServiceResult <Boolean> deleteCart(@RequestParam("cartId") Integer cartId) {
        cartService.deleteCart(cartId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "购物车列表")
    @GetMapping(value = "/cart")
    public ServiceResult <List <BaseCart>> cartList(@ApiIgnore Principal user) {
        return ServiceResult.success(cartService.cartList(user.getId()));
    }
}
